package net.uman.framework.modules.utils;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

public class Resource {
	private static Map<Locale, ResourceBundle> langs = new HashMap<Locale, ResourceBundle>();
	private static Map<Locale, ResourceBundle> messages = new HashMap<Locale, ResourceBundle>();
	
	public static String getString(String key) {
		Locale locale = Locale.getDefault();
		ResourceBundle bundle = langs.get(locale);
		if (bundle == null) {
			bundle = ResourceBundle.getBundle("net.b2bcenter.web.resource.lang", locale);
			langs.put(locale, bundle);
		}
		return bundle.getString(key);
	}

	public static String getString(String key, Object... args) {
		Locale locale = Locale.getDefault();
		ResourceBundle bundle = messages.get(locale);
		if (bundle == null) {
			bundle = ResourceBundle.getBundle("net.b2bcenter.web.resource.message", locale);
			messages.put(locale, bundle);
		}

		MessageFormat formatter = new MessageFormat("");
		formatter.setLocale(locale);
		formatter.applyPattern(bundle.getString(key));
		return formatter.format(args);
	}
}
